package com.service;

import com.dto.QueryDTO;
import com.dao.EmployeeDao;
import com.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    private EmployeeDao employeeDao;

    @Autowired
    private MailService mailService;

    @Autowired
    public EmployeeServiceImpl(EmployeeDao employeeDao){
        this.employeeDao = employeeDao;
    }



    @Override
    public void update(Employee employee) {
        employeeDao.update(employee);
    }

    @Override
    public void delete(Integer id) {
        employeeDao.delete(id);
    }

    @Override
    public List<Employee> findByPage(QueryDTO queryDTO) {
        // if(queryDTO.getPageNow() == null || queryDTO.getPageNow().length() <= 0)
        //string转int用Long.valueOf()
       queryDTO.setStart((queryDTO.getPageNow() - 1) * queryDTO.getPageSize());
          return employeeDao.findByPage(queryDTO);
    }

    @Override
    public String findTotals() {
        return employeeDao.findTotals();
    }


    @Override
    public Employee findById(Integer id) {
        return employeeDao.findById(id);
    }

    @Override
    public void save(Employee employee) {
        employeeDao.save(employee);
        String subject="欢迎加入xxxx公司";
        String text= "欢迎新员工在今日进入我司入职";
        mailService.sendSimpleEmail(employee.getEpemail(),subject,text);
        System.out.println("给员工发送邮件成功");
    }

    @Override
    public void saveAll(List<Employee> employees) {
        employees.forEach(employee -> {
            employee.setId(null);
         String fileName = employee.getPhoto().substring(employee.getPhoto().lastIndexOf("\\")+1);
         employee.setPhoto(fileName);
        employeeDao.save(employee);
        });
    }

    @Override
    public List<Employee> lists() {
        return employeeDao.lists();
    }
}
